package com.tony.mindwarning.controller;

import com.tony.mindwarning.pojo.Product;
import com.tony.mindwarning.service.WarnService;
import com.tony.mindwarning.utils.PageUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: WarnController
 * @Auther: 皮蛋布丁
 * @Date: 2021/04/11/15:44
 * @Description:
 */
@Controller
@Scope("prototype")
public class WarnController {
    @Resource
    WarnService warnService;

    /**
    * @Description: queryPage 预警商品列表(分页)
    * @Param: [pageIndex, pageSize, model]
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    */
    @RequestMapping("/warn_page")
    public String queryPage(@RequestParam(value = "pageIndex",defaultValue = "1") Integer pageIndex,
                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize, Model model){
        //总条数
        Integer totalCount = warnService.totalCount();
        Map<String,Object> map=new HashMap<String,Object>();
        //开始查询的位置
        map.put("startIndex",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        //每页的数据
        List<Product> products = warnService.getProductByPage(map);
        //封装数据
        PageUtils<Product> pageUtils=new PageUtils<Product>(pageIndex,pageSize,totalCount,products);
        System.out.println("分页工具类："+pageUtils);
        model.addAttribute("pageUtils",pageUtils);
        System.out.println(pageUtils);
        return "warning";
    }
}
